/*
 * CarFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package com.ruif.view;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import com.ruif.client.CarServiceImpl;
import com.ruif.model.Car;
import com.ruif.service.ICarService;
import com.ruif.util.DBUtil;
import com.ruif.util.StringUtil;

/**
 *
 * @author  __USER__
 */
public class CarFrame extends javax.swing.JInternalFrame
{
    /**
     * 序列号ID
     */
    private static final long serialVersionUID = 5000022608650957353L;

    DBUtil db = new DBUtil();

    ICarService carService = new CarServiceImpl();

    /** Creates new form CarFrame */
    public CarFrame()
    {
        initComponents();
        this.setLocation(200, 50);
        fillTable(null);
    }

    //GEN-BEGIN:initComponents
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents()
    {

        jLabel2 = new javax.swing.JLabel();
        jTextField_serachCarNo = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea_desc = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        jTextField_type = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField_price = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField_date = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTextField_carNo = new javax.swing.JTextField();
        jTextField_id = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable_car = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);

        jLabel2.setText("\u8f66\u724c\u53f7\uff1a");

        jButton1.setText("\u67e5\u8be2");
        jButton1.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory
                .createLineBorder(new java.awt.Color(0, 0, 0)), "\u8868\u5355\u64cd\u4f5c",
                javax.swing.border.TitledBorder.LEADING, javax.swing.border.TitledBorder.TOP, new java.awt.Font("宋体",
                        0, 12), new java.awt.Color(102, 102, 102)));
        jPanel1.setForeground(new java.awt.Color(102, 102, 102));

        jLabel1.setText("\u7f16 \u53f7\uff1a");

        jButton3.setText("\u5220\u9664");
        jButton3.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("\u4fee\u6539");
        jButton4.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel6.setText("\u63cf\u8ff0\uff1a");

        jTextArea_desc.setColumns(20);
        jTextArea_desc.setRows(5);
        jScrollPane2.setViewportView(jTextArea_desc);

        jLabel4.setText("\u7c7b \u578b\uff1a");

        jLabel3.setText("\u4ef7 \u683c\uff1a");

        jLabel5.setText("\u5165\u5e93\u65e5\u671f\uff1a");

        jLabel7.setText("\u8f66\u724c\u53f7\uff1a");

        jTextField_id.setEditable(false);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout
                .setHorizontalGroup(jPanel1Layout
                        .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jPanel1Layout
                                .createSequentialGroup()
                                .addContainerGap()
                                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                        .add(jLabel1).add(jLabel5).add(jLabel7))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jPanel1Layout
                                        .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                        .add(jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 94,
                                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .add(jTextField_carNo, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 94,
                                                Short.MAX_VALUE)
                                        .add(org.jdesktop.layout.GroupLayout.TRAILING, jTextField_id,
                                                org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                                        .add(jTextField_date, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 94,
                                                Short.MAX_VALUE))
                                .add(78, 78, 78)
                                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                        .add(jPanel1Layout.createSequentialGroup().add(10, 10, 10).add(jLabel4))
                                        .add(jPanel1Layout.createSequentialGroup().add(18, 18, 18).add(jLabel6)))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jPanel1Layout
                                        .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                        .add(jPanel1Layout
                                                .createSequentialGroup()
                                                .add(jPanel1Layout
                                                        .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                                        .add(jPanel1Layout
                                                                .createSequentialGroup()
                                                                .add(jTextField_type,
                                                                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                                                        75,
                                                                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                                .add(33, 33, 33)
                                                                .add(jLabel3)
                                                                .addPreferredGap(
                                                                        org.jdesktop.layout.LayoutStyle.RELATED)
                                                                .add(jTextField_price,
                                                                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                                                        70,
                                                                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                                        .add(jScrollPane2,
                                                                org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 224,
                                                                Short.MAX_VALUE)).addContainerGap(54, Short.MAX_VALUE))
                                        .add(jPanel1Layout
                                                .createSequentialGroup()
                                                .add(jButton4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 95,
                                                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                .addContainerGap()))));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(
                jPanel1Layout
                        .createSequentialGroup()
                        .addContainerGap()
                        .add(jPanel1Layout
                                .createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                .add(jLabel1)
                                .add(jTextField_type, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                        org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(jLabel3)
                                .add(jTextField_price, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                        org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(jLabel4)
                                .add(jTextField_id, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                        org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel1Layout
                                .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(jPanel1Layout
                                        .createSequentialGroup()
                                        .add(jPanel1Layout
                                                .createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                                .add(jLabel5)
                                                .add(jLabel6)
                                                .add(jTextField_date, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                                        org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                        .add(jPanel1Layout
                                                .createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                                .add(jLabel7)
                                                .add(jTextField_carNo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                                        org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                        org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 32, Short.MAX_VALUE)
                        .add(jPanel1Layout
                                .createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                .add(jButton4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25,
                                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).add(jButton3))
                        .addContainerGap()));

        jTable_car.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

        }, new String[]
        { "编号", "车牌号", "价格", "类型", "入库日期", "描述" }));
        jTable_car.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mousePressed(java.awt.event.MouseEvent evt)
            {
                jTable_carMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTable_car);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(
                layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout
                                .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                .add(layout
                                        .createSequentialGroup()
                                        .add(49, 49, 49)
                                        .add(jLabel2)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(jTextField_serachCarNo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                                62, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .add(54, 54, 54)
                                        .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 97,
                                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)
                                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                        org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(
                layout.createSequentialGroup()
                        .add(21, 21, 21)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(jLabel2)
                                .add(jTextField_serachCarNo).add(jButton1))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 138,
                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).addContainerGap()));

        pack();
    }// </editor-fold>
    //GEN-END:initComponents

    /**
     * 触发 删除事件
     * @param evt
     */
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
    {
        String id = this.jTextField_id.getText().trim();
        if (StringUtil.isEmpty(id))
        {
            JOptionPane.showMessageDialog(null, "请选择要删除的记录！");
            return;
        }
        
        Car car = new Car(Long.parseLong(id));
        int ok = carService.deleteCar(car);
        if (ok == 0)
        {
            JOptionPane.showMessageDialog(null, "删除失败");
            return;
        }
        if (ok == -1)
        {
            return;
        }
        
        JOptionPane.showMessageDialog(null, "删除成功！");
        this.resetValue();
        this.fillTable(null);
        
    }

    /**
     * 触发修改事件
     * @param evt
     */
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt)
    {
        String id = this.jTextField_id.getText().trim();
        String licenseNo = this.jTextField_carNo.getText().trim();
        String price = this.jTextField_price.getText().trim();
        String type = this.jTextField_type.getText().trim();
        String date = this.jTextField_date.getText().trim();
        String desc = this.jTextArea_desc.getText().trim();

        if (StringUtil.isEmpty(id))
        {
            JOptionPane.showMessageDialog(null, "请选择要修改的记录！");
            return;
        }
        if (StringUtil.isEmpty(licenseNo))
        {
            JOptionPane.showMessageDialog(null, "车牌号不能为空！");
            return;
        }
        if (desc.length() < 5)
        {
            JOptionPane.showMessageDialog(null, "请用5或>5个字描述");
            return;
        }
        Car car = new Car(Long.parseLong(id), licenseNo, Double.parseDouble(price), type, date, desc);

        int ok = carService.modifyCar(car);
        if (ok == 0)
        {
            JOptionPane.showMessageDialog(null, "修改失败！");
            return;
        }

        JOptionPane.showMessageDialog(null, "修改成功！");
        this.fillTable(null);
        this.resetValue();
    }

    private void resetValue()
    {
        this.jTextField_id.setText("");
        this.jTextField_carNo.setText("");
        this.jTextField_price.setText("");
        this.jTextField_type.setText("");
        this.jTextField_date.setText("");
        this.jTextArea_desc.setText("");
    }

    /**
     * 触发查询事件，接收一个 车牌号 参数
     * @param evt
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
    {
        String licenseNo = this.jTextField_serachCarNo.getText().trim();
        fillTable(licenseNo);
    }

    /**
     * 触发 mouse pressed事件
     * @param evt
     */
    private void jTable_carMousePressed(java.awt.event.MouseEvent evt)
    {
        int row = this.jTable_car.getSelectedRow();//此方法从 0 开始

        this.jTextField_id.setText(this.jTable_car.getValueAt(row, 0) + "");
        this.jTextField_carNo.setText(this.jTable_car.getValueAt(row, 1) + "");
        this.jTextField_price.setText(this.jTable_car.getValueAt(row, 2) + "");
        this.jTextField_type.setText(this.jTable_car.getValueAt(row, 3) + "");
        this.jTextField_date.setText(this.jTable_car.getValueAt(row, 4) + "");
        this.jTextArea_desc.setText(this.jTable_car.getValueAt(row, 5) + "");

    }

    public void fillTable(String licenseNo)
    {
        DefaultTableModel dtm = (DefaultTableModel) jTable_car.getModel();
        dtm.setRowCount(0);

        int ok = carService.listCar(dtm, licenseNo);
        if (ok == 0)
        {
            return;
        }
    }

    //GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable_car;
    private javax.swing.JTextArea jTextArea_desc;
    private javax.swing.JTextField jTextField_carNo;
    private javax.swing.JTextField jTextField_date;
    private javax.swing.JTextField jTextField_id;
    private javax.swing.JTextField jTextField_price;
    private javax.swing.JTextField jTextField_serachCarNo;
    private javax.swing.JTextField jTextField_type;
    // End of variables declaration//GEN-END:variables

}